/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsysacc.asistencia.model.dao.component.maestros;

import com.cerd.util.BaseSP01.JUtilBaseSP01;
import com.jsysacc.asistencia.model.beans.maestros.Parametro;
import com.jsysacc.asistencia.model.dao.design.maestros.IParametroDAO;
import com.jsysacc.asistencia.model.dao.ds.Cnx;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.joda.time.DateTime;

/**
 *
 * @author Carlos
 */
public class ParametroDAO extends JUtilBaseSP01 implements IParametroDAO {

    private Connection cnx = null;
    private PreparedStatement pst = null;
    private Statement stm = null;
    private ResultSet rs = null;
    private CallableStatement cs = null;

    @Override
    public boolean insert(Parametro entity) throws SQLException {
        String sql = " { ? = CALL " + Cnx.getSchema() + ".sp_parametros_insert(  ?, ? , ? ,?, ?, ?, ? ) } ";
        cnx = Cnx.getConnection();
        cnx.setAutoCommit(false);
        try {
            cs = cnx.prepareCall(sql);
            int pos=1;
            cs.registerOutParameter(pos++, Types.BOOLEAN);
            cs.setString(pos++, entity.getRuc_emp());
            cs.setString(pos++, entity.getNom_emp());
            cs.setString(pos++, entity.getNom_cor());
            cs.setString(pos++, entity.getDir_emp());
            cs.setString(pos++, entity.getTel_emp());
            cs.setString(pos++, entity.getMail_emp());
            cs.setString(pos++, entity.getLicencia());
            cs.execute();
            if (!cs.getBoolean(1)) {
                throw new SQLException("ERROR AL INSERTAR EL REGISTRO");
            }
            cnx.commit();
            return true;
        } catch (SQLException ex) {
            cnx.rollback();
            throw new SQLException(ex.getMessage());
        } finally {
            try {
                if (cs != null) {
                    cs.close();
                }
                if (cnx != null) {
                    cnx.close();
                }
            } catch (SQLException ex) {
                throw new SQLException(ex.getMessage());
            }
        }
    }

    @Override
    public boolean update(Parametro entity) throws SQLException {
        String sql = " { ? = CALL " + Cnx.getSchema() + ".sp_t_parametros_update( ?, ? , ? , ?, ? , ?, ? , ? ) } ";
        cnx = Cnx.getConnection();
        cnx.setAutoCommit(false);
        try {
            cs = cnx.prepareCall(sql);
            int pos=1;
            cs.registerOutParameter(pos++, Types.BOOLEAN);
            cs.setString(pos++, entity.getCod_emp());
            cs.setString(pos++, entity.getRuc_emp());
            cs.setString(pos++, entity.getNom_emp());
            cs.setString(pos++, entity.getNom_cor());
            cs.setString(pos++, entity.getDir_emp());
            cs.setString(pos++, entity.getTel_emp());
            cs.setString(pos++, entity.getMail_emp());
            cs.setString(pos++, entity.getLicencia());
            cs.execute();
            if (!cs.getBoolean(1)) {
                throw new SQLException("ERROR AL MODIFICAR EL REGISTRO");
            }
            cnx.commit();
            return true;
        } catch (SQLException ex) {
            cnx.rollback();
            throw new SQLException(ex.getMessage());
        } finally {
            try {
                if (cs != null) {
                    cs.close();
                }
                if (cnx != null) {
                    cnx.close();
                }
            } catch (SQLException ex) {
                throw new SQLException(ex.getMessage());
            }
        }
    }

    @Override
    public Parametro getByPk(String pk) throws SQLException {
        Parametro oParametro = null;
        String sql = " select * from " + Cnx.getSchema() + ".sp_t_parametros_get_by_pk(?) ; ";
        cnx = Cnx.getConnection();
        try {
            pst = cnx.prepareStatement(sql);
            pst.setString(1, pk);
            rs = pst.executeQuery();
            while (rs.next()) {
                oParametro = new Parametro();
                mapearBeanForRS(rs, oParametro);
            }
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (cnx != null) {
                    cnx.close();
                }
            } catch (SQLException ex) {
                throw new SQLException(ex.getMessage());
            }
        }

        return oParametro;
    }

    @Override
    public DateTime getNow() throws SQLException {
        DateTime fechaSistema = null;

        String sql = " select current_timestamp ; ";
        cnx = Cnx.getConnection();
        try {
            pst = cnx.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                fechaSistema = toDateTime(rs.getTimestamp("now"));
            }
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (cnx != null) {
                    cnx.close();
                }
            } catch (SQLException ex) {
                throw new SQLException(ex.getMessage());
            }
        }

        return fechaSistema;
    }

}
